#include "stdafx.h"
#include "listctrl_wrapper.h"
#include "TaTaMainDlg.h"
#include "page_main_wrapper.h"

listctrl_wrapper::listctrl_wrapper()
{

}

listctrl_wrapper::~listctrl_wrapper()
{

}

BEGIN_MESSAGE_MAP(listctrl_wrapper, CListCtrl)
	//{{AFX_MSG_MAP(CXListCtrl)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL listctrl_wrapper::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: Add your specialized code here and/or call the base class
	if ( message == WM_LISTCTRL_EVENT_LBTNDOWN )
	{
		
		CString str;
		int nitem = (int)wParam;
		CString strid = (LPCTSTR)lParam;

		map_ui_info::iterator iter = this->map_ui_info_.begin();
		for( ; iter != this->map_ui_info_.end(); iter++ )
		{
			if( nitem == iter->second.index )
			{
				iter->second.ischeck = !iter->second.ischeck;
				if( iter->second.ischeck )
				{
					dsSetDrawItemValueEx(GetSafeHwnd(), nitem, 1, "status", "#stardard.checkbtn.tick.nor", TRUE);
				}
				else
				{
					dsSetDrawItemValueEx(GetSafeHwnd(), nitem, 1, "status", "#stardard.checkbtn.nor", TRUE);
				}
				::PostMessage(dll_manager::instance()->get_main_obj()->impl_->page_main_->ui_mgr_->GetSafeHwnd(), WM_UI_MANAGER_CHANGE, 0, 0);
			}
		}
	}
	return CListCtrl::OnWndMsg(message, wParam, lParam, pResult);
}

ui_info_t listctrl_wrapper::get_ui_info(int index, int &commandid)
{
	//map_ui_info::iterator iter = this->map_ui_info_.find(index);
	map_ui_info::iterator iter = this->map_ui_info_.begin();
	for( ; iter != this->map_ui_info_.end(); iter++ )
	{
		if( iter->second.index == index )
		{
			commandid = iter->first;
			return iter->second;
		}
	}
	return ui_info_t();
}

BOOL listctrl_wrapper::up_item(int index)
{
	//ui_info_t info;
	map_ui_info::iterator iter = this->map_ui_info_.begin();
	map_ui_info::iterator pos1,pos2;
	for( ; iter != this->map_ui_info_.end(); iter++ )
	{
		if( index == iter->second.index )
		{
			pos1 = iter;
			
		}
		if( index - 1 == iter->second.index )
		{
			pos2 = iter;
		}
	}
	pos1->second.index = index - 1;
	pos2->second.index = index;
	::PostMessage(dll_manager::instance()->get_main_obj()->impl_->page_main_->ui_mgr_->GetSafeHwnd(), WM_UI_MANAGER_CHANGE, 0, 0);
	return TRUE;
}

BOOL listctrl_wrapper::down_item(int index)
{
	//ui_info_t info;
	map_ui_info::iterator iter = this->map_ui_info_.begin();
	map_ui_info::iterator pos1,pos2;
	for( ; iter != this->map_ui_info_.end(); iter++ )
	{
		if( index == iter->second.index )
		{
			pos1 = iter;
		}
		if( index + 1 == iter->second.index )
		{
			pos2 = iter;
		}
	}
	pos1->second.index = index + 1;
	pos2->second.index = index;
	::PostMessage(dll_manager::instance()->get_main_obj()->impl_->page_main_->ui_mgr_->GetSafeHwnd(), WM_UI_MANAGER_CHANGE, 0, 0);
	return TRUE;
	
}

BOOL listctrl_wrapper::delete_item(int index)
{
	map_ui_info::iterator iter = this->map_ui_info_.begin();
	for( ; iter != this->map_ui_info_.end(); iter++ )
	{
		if( index == iter->second.index )
		{
			iter = this->map_ui_info_.erase(iter);
			break;;
		}
	}
	iter = this->map_ui_info_.begin();
	for( ; iter != this->map_ui_info_.end(); iter++ )
	{
		if( iter->second.index > index )
		{
			iter->second.index--;
		}
	}
	return TRUE;
}